package 协议;

import java.io.IOException;
import java.net.*;
import java.util.Scanner;

public class UdpClient {
    private DatagramSocket socket=null;
    private String serverip;
    private int port;

    public UdpClient(String serverip, int port) throws SocketException {
        socket=new DatagramSocket();
        this.serverip = serverip;
        this.port = port;
    }
    public void start() throws IOException {
        while (true) {
            System.out.println("客户端上线");
            Scanner scanner=new Scanner(System.in);
            System.out.println("请输入第一个数");
            int num1=scanner.nextInt();
            System.out.println("请输入第二个数");
            int num2=scanner.nextInt();
            System.out.println("请输入运算符");
            String s=scanner.next();
            String request=num1+";"+num2+";"+s;
            DatagramPacket datagramPacket=new DatagramPacket(request.getBytes(),request.getBytes().length
                                                            ,InetAddress.getByName(serverip),port);
            socket.send(datagramPacket);
            DatagramPacket response=new DatagramPacket(new byte[4096],4096);
            socket.receive(response);
            String str=new String(response.getData(),0,response.getLength());
            System.out.println(str);
        }
    }

    public static void main(String[] args) throws IOException {
        UdpClient udpClient=new UdpClient("127.0.0.1",9090);
        udpClient.start();
    }
}
